package com.py.framework.service.impl;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.framework.common.Constants;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.utils.DateUtils;
import com.py.framework.dao.RoleDao;
import com.py.framework.entity.RoleEntity;
import com.py.framework.service.RoleService;

/**
 * 角色管理Service实现
 * @author Leegern
 * @date   2018年4月26日
 */
@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleDao roleDao;
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#queryRolesForPage(java.util.Map)
	 */
	@Override
	public List<RoleEntity> queryRolesForPage(Map<String, Object> params) {
		return roleDao.queryRolesForPage(params);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#findOneRole(long)
	 */
	@Override
	public RoleEntity findOneRole(long id) {
		if (id > Constants.NUMBER_ZERO) {
			return roleDao.findOneRole(id);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#getRoleMenuRelate(long)
	 */
	@Override
	public List<RoleEntity> getRoleMenuRelate(long roleId) {
		if (roleId > Constants.NUMBER_ZERO) {
			return roleDao.getRoleMenuRelate(roleId);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#getRoleUserRelate(long)
	 */
	@Override
	public List<RoleEntity> getRoleUserRelate(long roleId) {
		if (roleId > Constants.NUMBER_ZERO) {
			return roleDao.getRoleUserRelate(roleId);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#addRoleInfo(com.py.framework.entity.RoleEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addRoleInfo(RoleEntity entity) {
		entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		entity.setDelFlag(Constants.NUMBER_ONE);
		entity.setRoleStatus(Constants.NUMBER_TWO);
		return roleDao.addRoleInfo(entity);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#addRoleUserRelate(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addRoleUserRelate(List<RoleEntity> datas) {
		if (! CollectionUtils.isEmpty(datas)) {
			return roleDao.addRoleUserRelate(datas);
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#modifyRoleUserRelate(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int modifyRoleUserRelate(List<RoleEntity> datas) {
		if (! CollectionUtils.isEmpty(datas)) {
			RoleEntity role = datas.get(0);
			if (role.getId() >= Constants.NUMBER_ZERO) {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("userId", role.getUserId());
				// 删除角色用户信息
				roleDao.delRoleUserRelate(params);
				// 添加角色用户信息
				return roleDao.addRoleUserRelate(datas);
			}
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#addRoleMenuRelate(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addRoleMenuRelate(List<RoleEntity> datas) {
		if (! CollectionUtils.isEmpty(datas)) {
			return roleDao.addRoleMenuRelate(datas);
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#modifyRoleMenuRelate(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int modifyRoleMenuRelate(List<RoleEntity> datas) {
		if (! CollectionUtils.isEmpty(datas)) {
			RoleEntity role = datas.get(0);
			if (role.getId() > Constants.NUMBER_ZERO) {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("roleId", role.getId());
				if(datas.size()==1) {
					roleDao.delRoleMenuRelate(params);
				}else {
					roleDao.delRoleMenuRelate(params);
					return roleDao.addRoleMenuRelate(datas);
				}
			}
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#addRoleOfficeRelate(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addRoleOfficeRelate(List<RoleEntity> datas) {
		if (! CollectionUtils.isEmpty(datas)) {
			return roleDao.addRoleOfficeRelate(datas);
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#modifyRoleOfficeRelate(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int modifyRoleOfficeRelate(List<RoleEntity> datas) {
		if (! CollectionUtils.isEmpty(datas)) {
			RoleEntity role = datas.get(0);
			if (role.getId() > Constants.NUMBER_ZERO) {
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("roleId", role.getId());
				roleDao.delRoleOfficeRelate(params);
				return roleDao.addRoleOfficeRelate(datas);
			}
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#updateRoleInfo(com.py.framework.entity.RoleEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int updateRoleInfo(RoleEntity entity) {
		entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		return roleDao.updateRoleInfo(entity);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#batchDelRoles(java.util.List)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int batchDelRoles(List<Long> ids) {
		if (! CollectionUtils.isEmpty(ids)) {
			List<RoleEntity> roles = null;
			for (Long roleId : ids) {
				// 角色菜单
				roles = roleDao.getRoleMenuRelate(roleId);
				if (! CollectionUtils.isEmpty(roles)) {
					throw new ServiceException("已关联菜单，删除失败！");
				}
				roles = roleDao.getRoleUserRelate(roleId);
				if (! CollectionUtils.isEmpty(roles)) {
					throw new ServiceException("已关联用户，删除失败！");
				}
				roles = roleDao.getRoleOfficeRelate(roleId);
				if (! CollectionUtils.isEmpty(roles)) {
					throw new ServiceException("已关联部门，删除失败！");
				}
			}
			Map<String, Object> params = new HashMap<>();
			params.put("ids", ids);
			params.put("updateTime", new Timestamp(DateUtils.getCurrentDateTime().getTime()));
			return roleDao.batchDelRoles(params);
		}
		return 0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#getAllRoles()
	 */
	@Override
	public List<RoleEntity> getAllRoles() {
		return roleDao.getAllRoles();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#getRolesByName(long, java.lang.String)
	 */
	@Override
	public List<RoleEntity> getRolesByName(long roleId, String roleName) {
		if (StringUtils.isNotBlank(roleName)) {
			return roleDao.getRolesByName(roleId, roleName);
		}
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#delRoleUserRelate(java.util.Map)
	 */
	@Override
	public int delRoleUserRelate(Map<String, Object> params) {
		return roleDao.delRoleUserRelate(params);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#delRoleMenuRelate(java.util.Map)
	 */
	@Override
	public int delRoleMenuRelate(Map<String, Object> params) {
		return roleDao.delRoleMenuRelate(params);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#delRoleOfficeRelate(java.util.Map)
	 */
	@Override
	public int delRoleOfficeRelate(Map<String, Object> params) {
		return roleDao.delRoleOfficeRelate(params);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.RoleService#getRolesByUserId(long)
	 */
	@Override
	public List<RoleEntity> getRolesByUserId(long userId) {
		if (userId > Constants.NUMBER_ZERO) {
			return roleDao.getRolesByUserId(userId);
		}
		return null;
	}
}